package com.sky.financelake.ui.activity;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.sky.financelake.R;
import com.sky.financelake.base.BaseA;
import com.sky.financelake.databinding.ActivityMainBinding;
import com.sky.financelake.ui.fragment.ChooseF;
import com.sky.financelake.ui.fragment.CommunityF;
import com.sky.financelake.ui.fragment.HomeF;
import com.sky.financelake.ui.fragment.MyF;

import dagger.hilt.android.AndroidEntryPoint;


/**
 * ////////////////////////////////////////////////////////////////////
 * //                          _ooOoo_                               //
 * //                         o8888888o                              //
 * //                         88" . "88                              //
 * //                         (| ^_^ |)                              //
 * //                         O\  =  /O                              //
 * //                      ____/`---'\____                           //
 * //                    .'  \\|     |//  `.                         //
 * //                   /  \\|||  :  |||//  \                        //
 * //                  /  _||||| -:- |||||-  \                       //
 * //                  |   | \\\  -  /// |   |                       //
 * //                  | \_|  ''\---/''  |   |                       //
 * //                  \  .-\__  `-`  ___/-. /                       //
 * //                ___`. .'  /--.--\  `. . ___                     //
 * //              ."" '<  `.___\_<|>_/___.'  >'"".                  //
 * //            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                 //
 * //            \  \ `-.   \_ __\ /__ _/   .-` /  /                 //
 * //      ========`-.____`-.___\_____/___.-`____.-'========         //
 * //                           `=---='                              //
 * //      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //
 * //         佛祖保佑       永无BUG     永不修改                  //
 * ////////////////////////////////////////////////////////////////////
 */
@AndroidEntryPoint
public class MainA extends BaseA {

    private ActivityMainBinding binding;
    private Fragment homeF, chooseF, communityF, myF, preFragment, curFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        homeF = HomeF.newInstance();
        chooseF = ChooseF.newInstance();
        communityF = CommunityF.newInstance();
        myF = MyF.newInstance();

        initFragment();
        BottomNavigationView navigationView = binding.navigationView;
        navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        curFragment = homeF;
                        break;

                    case R.id.navigation_choose:
                        curFragment = chooseF;
                        break;

                    case R.id.navigation_community:
                        curFragment = communityF;
                        break;

                    case R.id.navigation_my:
                        curFragment = myF;
                        break;
                }
                boolean status = loadFragment(preFragment, curFragment);
                preFragment = curFragment;

                return status;
            }
        });


    }

    public void initFragment() {
        preFragment = homeF;
        curFragment = homeF;
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, homeF)
                .add(R.id.fragment_container, chooseF)
                .add(R.id.fragment_container, communityF)
                .add(R.id.fragment_container, myF)
                .show(homeF)
                .hide(chooseF)
                .hide(communityF)
                .hide(myF)
                .commitAllowingStateLoss();
    }


    private boolean loadFragment(Fragment o, Fragment n) {
        //switching fragment
        if (o != null && n != null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .hide(o)
                    .show(n)
                    .commitAllowingStateLoss();
            return true;
        }
        return false;
    }


}